"""
新建StsClient，并使用获得的临时token，配置BceClientConfiguration。
"""
from baidubce.services.sts.sts_client import StsClient
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.auth.bce_credentials import BceCredentials

from django.conf import settings

# 注意：目前使用STS配置client时，无论对应BOS服务的endpoint在哪里，
# endpoint都需配置为http://sts.bj.baidubce.com。
sts_host = 'http://sts.bj.baidubce.com'
access_key_id = settings.BOS_ACCESS_KEY_ID
secret_access_key = settings.BOS_SECRET_ACCESS_KEY

sts_sample_config = BceClientConfiguration(
    credentials=BceCredentials(access_key_id, secret_access_key),
    endpoint=sts_host
)

sts_client = StsClient(sts_sample_config)

# 临时密钥有效时长，单位是秒（30分钟=1800秒）
duration_seconds = 1800
# you can specify limited permissions with ACL
access_dict = {"service": "bce:bos", "region": "fsh", "effect": "Allow"}
resource = ["*"]
access_dict["resource"] = resource
permission = ["READ"]
access_dict["permission"] = permission

access_control_list = {"accessControlList": [access_dict]}
# 新建StsClient
response = sts_client.get_session_token(acl=access_control_list, duration_seconds=duration_seconds)

sts_ak = str(response.access_key_id)
sts_sk = str(response.secret_access_key)

token = response.session_token


# bos_host = "BOS_HOST"
# bos_host = 'https://fsh.bcebos.com'
# 配置BceClientConfiguration
# config = BceClientConfiguration(
#     credentials=BceCredentials(sts_ak, sts_sk),
#     endpoint=bos_host,
#     security_token=token
# )

